﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

/// <summary>
/// Summary description for PaymentMethodDetail
/// </summary>
public class ManagementPaymentDetail
{
    SqlConnection coon;
    public ManagementPaymentDetail()
	{
        coon = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
	}

    public DataTable GetPaymentMethod() {
        if (coon.State == ConnectionState.Closed)
        {
            coon.Open();
        }
        DataTable dt = new DataTable();
        string sql = "select * from PaymentMethod";
        SqlDataAdapter sda = new SqlDataAdapter(sql, coon);
        sda.Fill(dt);
        return dt;
    }

    public DataTable GetPaymentDetail()
    {
        if (coon.State == ConnectionState.Closed)
        {
            coon.Open();
        }
        DataTable dt = new DataTable();
        string sql = "select * from PaymentMethodDetail left join PaymentMethod on(PaymentMethod.PaymentMethodID=PaymentMethodDetail.PaymentMethodID) order by DetailID DESC";
        SqlDataAdapter sda = new SqlDataAdapter(sql, coon);
        sda.Fill(dt);
        return dt;
    }

    public List<int> GetPaymentDetailID()
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        List<int> l = new List<int>();

        string sql = "select DetailID FROM PaymentMethodDetail";
        SqlCommand cmd = new SqlCommand(sql, coon);
        SqlDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            l.Add(sdr.GetInt32(0));
        }
        sdr.Close();
        coon.Close();
        return l;
    }
    public bool DeletePaymentDetail(string id)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();

        string sql = "delete from PaymentMethodDetail where DetailID='"+id+"'";
        SqlCommand cmd = new SqlCommand(sql, coon);
        int i = cmd.ExecuteNonQuery();

        coon.Close();
        return i > 0 ? true : false;
    }

    public DataTable SearchPaymentDetail(string s)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "select * from PaymentMethodDetail,PaymentMethod where PaymentMethod.PaymentMethodID=PaymentMethodDetail.PaymentMethodID and PaymentMethodDetail.DetailName like '%"+s+"%' order by DetailID DESC";
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(sql, coon);
        sda.Fill(dt);

        return dt;
    }


    public bool UpdatePaymentDetail(string id, string name, double charges, string note, int paymentmethodID)
    {
        if (coon.State == ConnectionState.Closed)
        { coon.Open(); }

        string sql = "update PaymentMethodDetail set DetailName='" + name + "',PriceCharges=" + charges + ",Note='" + note + "',PaymentMethodID=" + paymentmethodID + " where DetailID='" + id + " '";
        SqlCommand cmd = new SqlCommand(sql, coon);


        int i = cmd.ExecuteNonQuery();
        coon.Close();
        return i > 0 ? true : false;
    }


    public bool AddPaymentDetail(string name, double charges, string note, int paymentmethodID)
    {

        if (coon.State == ConnectionState.Closed)
            coon.Open();
        string sql = "insert into PaymentMethodDetail values(@n,@c,@note,@paymentID)";
        SqlCommand cmd = new SqlCommand(sql, coon);
        cmd.Parameters.AddWithValue("@n", name);
        cmd.Parameters.AddWithValue("@c", charges);
        cmd.Parameters.AddWithValue("@note", note);
        cmd.Parameters.AddWithValue("@paymentID", paymentmethodID);
        int i = cmd.ExecuteNonQuery();
        coon.Close();
        return i > 0 ? true : false;
    }
    public ORANGE_PROJECTModel.PaymentMethodDetail GetEditPaymentDetail(string id)
    {
        if (coon.State == ConnectionState.Closed)
            coon.Open();
        DataTable dt = new DataTable();
        string sql = "select * from PaymentMethodDetail where DetailID="+id+"";
        SqlCommand cmd = new SqlCommand(sql, coon);
        SqlDataReader sda = cmd.ExecuteReader();
        ORANGE_PROJECTModel.PaymentMethodDetail c = new ORANGE_PROJECTModel.PaymentMethodDetail();
        while (sda.Read())
        {
            c.DetailID = sda.GetInt32(0);
            c.DetailName = sda.GetString(1);
            c.PriceCharges = sda.GetDouble(2);
            c.Note = sda.GetString(3);
            c.PaymentMethodID = sda.GetInt32(4);
        }
        coon.Close();
        return c;
    }

    
}